home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / icons / animator.zip / FILEIO.C < prev    next >
C/C++ Source or Header  |  1993-04-16  |  10KB  |  330 lines

  1. #include "animator.h"
  2.  
  3. short NEAR PASCAL WritePrivateProfileInt (LPSTR, LPSTR, short, LPSTR);
  4.  
  5. //////////////////////////////////////////////////////////////////////////
  6. // ShowCommonDialog - sets up the OPENFILE data structure, and 
  7. // lets the COMMDLG dll fire away. returns true if user hits ok.
  8. //////////////////////////////////////////////////////////////////////////
  9.  
  10. BOOL WINAPI ShowCommonDialog (HWND hwnd, LPSTR * lpszFilter, 
  11.     LPSTR lpszFile, LPSTR lpszTitle, LPSTR lpszDefExt, BOOL fSave)
  12. {
  13.     OPENFILENAME     ofn;
  14.     BOOL             fResult;
  15.  
  16.     memset ((VOID *)&ofn, 0x00, sizeof(OPENFILENAME));
  17.  
  18.     ofn.lStructSize = sizeof (OPENFILENAME);
  19.     ofn.hwndOwner = hwnd;
  20.     ofn.lpstrFilter = (LPSTR)lpszFilter[0];
  21.     ofn.nFilterIndex = 0L;
  22.     ofn.lpstrFile = lpszFile;
  23.     ofn.nMaxFile = MAX_FILE_SIZE;
  24.     ofn.lpstrTitle = (LPSTR)lpszTitle;
  25.     ofn.lpstrDefExt = lpszDefExt;
  26.  
  27.     if (fSave)
  28.     {
  29.         ofn.Flags = OFN_OVERWRITEPROMPT;
  30.         fResult = GetSaveFileName((LPOPENFILENAME)&ofn);
  31.     }
  32.     else
  33.     {
  34.         fResult = GetOpenFileName((LPOPENFILENAME)&ofn);
  35.     }
  36.  
  37.     return fResult;
  38. }
  39.  
  40.  
  41. //////////////////////////////////////////////////////////////////////////
  42. // SaveIconsToFile - basically handles all of the file i/o to the
  43. // script file specified.  Records the time interval, number of 
  44. // icons, and icon file pathnames in .INI file format.
  45. //////////////////////////////////////////////////////////////////////////
  46.  
  47. VOID WINAPI SaveIconsToFile (HWND hwnd, WORD wAction)
  48. {
  49.     char szInt[16], _szIconPath[MAX_FILE_SIZE];
  50.     short i, sChild;
  51.  
  52.     if (!IsWindow) return;
  53.  
  54.     sChild = WINDOWNUM(hwnd);
  55.  
  56.     if ( (wAction == IDM_SAVEAS) || (!SZFILENAME(sChild)[0]))
  57.     {
  58.         lstrcpy ((LPSTR)_szIconPath, (LPSTR)SZFILENAME(sChild));
  59.  
  60.         if (!ShowCommonDialog(_hwndFrame, _lpszScriptFilter, (LPSTR)_szIconPath, 
  61.             (LPSTR)"File Save As...\0", (LPSTR)_szExtension, TRUE))
  62.         {
  63.             goto Err;
  64.         }
  65.  
  66.         lstrcpy ((LPSTR)SZFILENAME(sChild), (LPSTR)_szIconPath);
  67.  
  68.         SetWindowText (hwnd, (LPSTR)_szIconPath);
  69.     }
  70.  
  71.     // Write the "Info" application section...
  72.     
  73.     if (!WritePrivateProfileInt ((LPSTR)_szInfo, (LPSTR)_szTimeInt, 
  74.         TIMEINT(sChild), (LPSTR)SZFILENAME(sChild)))
  75.     { 
  76.         MESSAGE (IDS_TimeIntervalErr);
  77.         goto Err; 
  78.     }
  79.  
  80.     if (!WritePrivateProfileInt ((LPSTR)_szInfo, (LPSTR)_szNumIcons,
  81.         NUMICONS(sChild), (LPSTR)SZFILENAME(sChild)))
  82.     { 
  83.         MESSAGE (IDS_NumInterval);
  84.         goto Err; 
  85.     }
  86.  
  87.     if (!WritePrivateProfileInt ((LPSTR)_szInfo, (LPSTR)_szAutoAnimateKey,
  88.         (short)AUTOANIMATE(sChild), (LPSTR)SZFILENAME(sChild)))
  89.     { 
  90.         MESSAGE (IDS_AutoAnimateErr);
  91.         goto Err; 
  92.     }
  93.  
  94.     if (!WritePrivateProfileString ((LPSTR)_szInfo, (LPSTR)_szLinkFile,
  95.         (LPSTR)SZEXELINK(sChild), (LPSTR)SZFILENAME(sChild)))
  96.     { 
  97.         MESSAGE (IDS_LinkFileErr);
  98.         goto Err; 
  99.     }
  100.  
  101.     // First, lets delete the entire [Icons] section using the
  102.     // function WritePrivateProfileString.  This gets rid of 
  103.     // extra Icon#= keynames if less icons are being saved.
  104.  
  105.     if (!WritePrivateProfileString ((LPSTR) _szIconSection, (LPSTR)NULL,
  106.             (LPSTR)NULL,(LPSTR) SZFILENAME(sChild)))
  107.     { 
  108.         MESSAGE (IDS_IconsDelete);
  109.         goto Err; 
  110.     }
  111.  
  112.     // Write the "Icons" application section...
  113.  
  114.     for (i=1 ; i<=(short)NUMICONS(sChild) ; i++)
  115.     {
  116.         wsprintf ((LPSTR)szInt,"%s%d", (LPSTR)_szIcon, i);
  117.  
  118.         ListBox_GetText (HWNDLIST(hwnd), i-1, (LPSTR)_szIconPath);
  119.         
  120.         if (!WritePrivateProfileString ((LPSTR)_szIconSection,(LPSTR)szInt,
  121.             (LPSTR)_szIconPath,(LPSTR)SZFILENAME(sChild)))
  122.         { 
  123.             MESSAGE (IDS_IconsSetting);
  124.             goto Err; 
  125.         }
  126.     }
  127.  
  128.     SetWindowText (hwnd, (LPSTR)SZFILENAME(sChild));
  129.  
  130. Err:
  131.     return ;
  132. }
  133.  
  134.  
  135.  
  136. //////////////////////////////////////////////////////////////////////////
  137. // OpenIconsInFile - displays the open dialog box, then does the 
  138. // necessary file i/o to fill the listbox with the icon file 
  139. // pathnames and timer interval, and animStruct data structure stuff.
  140. //////////////////////////////////////////////////////////////////////////
  141.  
  142. BOOL  WINAPI OpenIconsInFile (char * szFileName)
  143. {
  144.     short  i, sChild;
  145.     BOOL   result=TRUE;    
  146.     HWND   hwnd;
  147.  
  148.  
  149.     if (szFileName[0] == '\0')   // if no file is specified to open:
  150.     {
  151.         getcwd (szFileName, sizeof (szFileName));
  152.  
  153.         if (!ShowCommonDialog(_hwndFrame,_lpszScriptFilter,
  154.             (LPSTR)szFileName, (LPSTR)"File Open...\0", 
  155.             (LPSTR)_szExtension, FALSE))
  156.         {
  157.             result = FALSE;
  158.             goto Err;
  159.         }
  160.     }
  161.  
  162.     // Get the window up and find out mdi window number...
  163.     SendMessage (_hwndFrame, WM_COMMAND, IDM_NEW, 0L);
  164.  
  165.     hwnd = MDI_GetActive (_hwndClient);
  166.  
  167.     if (!IsWindow(hwnd)) return FALSE;
  168.  
  169.     ShowWindow (hwnd, SW_SHOWMAXIMIZED);
  170.  
  171.     sChild = WINDOWNUM (hwnd);
  172.     
  173.     lstrcpy ((LPSTR)SZFILENAME(sChild), (LPSTR)szFileName);
  174.  
  175.     SetWindowText (hwnd, (LPSTR)szFileName);    
  176.  
  177.  
  178.     // Get the "TimerInterval=" setting and make sure it is valid:
  179.  
  180.     SET_TIMEINT (sChild, (short)GetPrivateProfileInt ((LPSTR)_szInfo,
  181.         (LPSTR)_szTimeInt, 0, (LPSTR)szFileName)) ;
  182.  
  183.     if (TIMEINT(sChild) < 20 || TIMEINT(sChild) > 20000)
  184.     {
  185.         MESSAGE (IDS_TimeIntRange);
  186.     }
  187.  
  188.     // Get the "NumIcons=" setting and make sure it is valid:
  189.  
  190.     SET_NUMICONS (sChild, (short)GetPrivateProfileInt ((LPSTR)_szInfo,
  191.         (LPSTR)_szNumIcons, 0, (LPSTR)szFileName));
  192.  
  193.     if (NUMICONS(sChild) == 0)
  194.     {
  195.         MESSAGE (IDS_NumIconsErr);
  196.     }
  197.     
  198.     GetPrivateProfileString ((LPSTR)_szInfo, (LPSTR)_szLinkFile, (LPSTR)"\0", 
  199.         (LPSTR)SZEXELINK(sChild), MAX_FILE_SIZE, (LPSTR)szFileName);
  200.  
  201.     if (!GetPathIfNoPath ((LPSTR)SZEXELINK(sChild)))
  202.     {
  203.         MESSAGE (IDS_InvalidPath);
  204.     }
  205.  
  206.     // Read the "Icons" application section...
  207.  
  208.     for (i=1 ; i<=(short)NUMICONS(sChild) ; i++)
  209.     {
  210.         char _szIconPath [MAX_FILE_SIZE];
  211.         char   szInt[28];
  212.  
  213.         wsprintf ((LPSTR)szInt, "%s%d", (LPSTR)_szIcon, i);
  214.         
  215.         GetPrivateProfileString ((LPSTR)_szIconSection, (LPSTR)szInt, (LPSTR)"\0",
  216.             (LPSTR)_szIconPath, MAX_FILE_SIZE, (LPSTR)SZFILENAME(sChild));
  217.  
  218.         if (_szIconPath[0] == '\0')
  219.         { 
  220.             MESSAGE (IDS_ListBoxUpdate);
  221.             MDI_Destroy (_hwndClient, MDI_GetActive(_hwndClient));
  222.             result = FALSE;
  223.             goto Err;
  224.         }
  225.  
  226.         if (!GetPathIfNoPath ((LPSTR)_szIconPath))
  227.         {
  228.             MESSAGE (IDS_InvalidFile);
  229.         }
  230.         else
  231.         {
  232.             ListBox_AddString (HWNDLIST(hwnd),(LPSTR)_szIconPath);
  233.         }            
  234.     }
  235.     
  236.     SetupIconHandles (hwnd);
  237.  
  238.     // Do this last, because setting ISANIMATE to TRUE starts
  239.     // animation immediately if everything is right.
  240.  
  241.     SET_AUTOANIMATE (sChild, (short)GetPrivateProfileInt ((LPSTR)_szInfo,
  242.         (LPSTR)_szAutoAnimateKey, 0, (LPSTR)szFileName));
  243.  
  244.     if (AUTOANIMATE(sChild))
  245.     {
  246.         SET_ISANIMATING(sChild, TRUE);
  247.     }
  248.  
  249. Err:
  250.     return result;
  251. }
  252.  
  253.  
  254. //////////////////////////////////////////////////////////////////////////
  255. // LinkToExecutable - displays the open dialog box, allowing the 
  256. // user to choose the executable in which to permanently link the
  257. // executable or com file to.
  258. //////////////////////////////////////////////////////////////////////////
  259.  
  260. BOOL WINAPI LinkToExecutable (LPSTR lpszFile)
  261. {
  262.     char szExePath [MAX_FILE_SIZE];
  263.     char szDlgTitle [32];
  264.     BOOL fResult;
  265.  
  266.     if (lpszFile[0] == '\0')
  267.     {
  268.         GetWindowsDirectory ((LPSTR)szExePath, MAX_FILE_SIZE);
  269.     }
  270.     else
  271.     {
  272.         lstrcpy ((LPSTR)szExePath, lpszFile);
  273.     }
  274.  
  275.     LoadStr (IDS_LinkDlgTitle, szDlgTitle);
  276.  
  277.     if (fResult =  ShowCommonDialog (_hwndFrame, _lpszEXEFilter, 
  278.         (LPSTR)szExePath, (LPSTR)szDlgTitle, 
  279.         (LPSTR)"*.EXE", FALSE))
  280.     {
  281.         lstrcpy (lpszFile, (LPSTR)szExePath);
  282.     }
  283.  
  284.     return fResult;
  285. }
  286.  
  287.  
  288. ///////////////////////////////////////////////////////////////////
  289. // WriteProfileInt - Writes a short integer to the INI file
  290. // specified.  I don't know why the Windows group didn't do this!
  291. ///////////////////////////////////////////////////////////////////
  292.  
  293. short NEAR PASCAL WritePrivateProfileInt (LPSTR lpAppName, LPSTR lpKeyName, 
  294.     short sData, LPSTR lpFileName)
  295. {
  296.     char szInt[8];
  297.  
  298.     wsprintf ((LPSTR)szInt,"%d", sData);
  299.  
  300.     return (short) WritePrivateProfileString (lpAppName,lpKeyName,
  301.         (LPSTR)szInt, lpFileName);
  302. }
  303.  
  304.  
  305. ///////////////////////////////////////////////////////////////////
  306. // GetPathIfNoPath() - This is the checknbalance function to
  307. // make sure that the path specified either on the command line
  308. // or in the animation script is valid.  If no path, it guesses.
  309. ///////////////////////////////////////////////////////////////////
  310.  
  311. BOOL WINAPI GetPathIfNoPath (LPSTR lpszName)
  312. {
  313.     OFSTRUCT of;
  314.  
  315.     short ret = OpenFile (lpszName, (LPOFSTRUCT)&of, OF_EXIST);
  316.  
  317.     if (ret < 0)
  318.     {
  319.         lpszName[0] = '\0';
  320.         return FALSE;
  321.     }
  322.     
  323.     if (lstrcmp (lpszName, (LPSTR)of.szPathName) != 0)
  324.     {
  325.         lstrcpy (lpszName, (LPSTR)of.szPathName);
  326.     }
  327.     
  328.     return TRUE;
  329. }
  330.